/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author ADMIN
 */
public class HandleDataGeo {
    public static final String KEYSPACE                         =   "AdsMobileDataResultKs";
    public static final String ColumFamly                       =   "BannerViewCountCf";
    public static final String ColumFamlyBannerClickCountCf     =   "BannerClickCountCf";
    public static final String ColumFamlyBannerViewCountCf      =   "BannerViewCountCf";

    public static final String ColumFamlyCampaignClickCountCf   =   "CampaignCountCf";
    public static final String ColumFamlyCampaignViewCountCf    =   "CampaignViewCountCf";

    public static final String ColumFamlyDomainDateCf           =   "BzSuperBannerDomainDateCf";
    public static final String ColumFamlyZoneDateCf             =   "BzSuperBannerZoneDateCf";
    public static final String BzSuperBannerDomain6HDateCf      =   "BzSuperBannerDomain6HDateCf";
    public static final String BzSuperBannerGeo                 =   "BannerTimestampGeoViewCountSuperCf";
    public static long checkDateDomainDate = 0;

    public static Map<String, String> handleData() throws InterruptedException, ParseException, ClassNotFoundException, InstantiationException, IllegalAccessException{
        
        CassandraAction cassAct = new CassandraAction(ACCOUNT.CLUSTERNAME ,ACCOUNT.URL, ACCOUNT.PORT);
        cassAct.initReadWirte(KEYSPACE);
        
        MysqlDataManagerMain mysqlMain = new MysqlDataManagerMain();
        //String currentDate = handleDate();
        //tinh từ 6h sáng
        String currentDate = "6:00:00 "+handleDate();
        //chuyen sang kieu long
        long startTime = TimeUtil.longToDateWithHourBanner(currentDate);
        //thoi gian ket thuc la 6h ngay hom sau
        long finishTime = startTime + 24*60*60*1000;
                
        List banerCPCandCPCPlus = new ArrayList();
        banerCPCandCPCPlus = mysqlMain.getBannerIDByMobile();
        Iterator<String> iterator = banerCPCandCPCPlus.iterator();
        Map<String, String> geoMap = new HashMap<>();
        
	while (iterator.hasNext()) {
            String bannerid = iterator.next();
            //String bannerid = "190477";
            String valueGeo = cassAct.getGeo(bannerid, BzSuperBannerGeo, startTime, finishTime);
            //Debug.println(bannerid +":"+valueGeo);
            geoMap.put(bannerid, valueGeo);
            /*String bannerid = iterator.next();
            //String bannerid = "185778";
            ThreadGeo tg = new ThreadGeo(cassAct, geoMap, bannerid, startTime, finishTime);*/
            //Thread.sleep(50000);
	}       
        mysqlMain.shutdownConnection();        
        return geoMap;
    }

    public static String handleDate()
    {
        String retDate = "";//2012-07-24
        String date = TimeUtil.getCurrentTimeInStringForLog();//yyyy/MM/dd H:mm:ss

        String[] arrTime        = new String[2];
        String[] firstArrTime   = new String[3];
        String[] secondArrTime  = new String[3];

        arrTime = date.split(" ");
        firstArrTime    = arrTime[1].split(":");
        secondArrTime   = arrTime[0].split("-");
        if(Integer.valueOf(firstArrTime[0]) >= 7 && Integer.valueOf(firstArrTime[0]) <= 23)
        {
            retDate = secondArrTime[0]+"-"+secondArrTime[1]+"-"+secondArrTime[2];
        }
        //init checkDate if trigger from 0h -> 6h
        else if (Integer.valueOf(firstArrTime[0]) >= 0 && Integer.valueOf(firstArrTime[0]) < 7 && TimeUtil.getCurrentTime() >= checkDateDomainDate)
        {
            //Debug.println(date + "_" + TimeUtil.getCurrentTime() + "_" + checkDateDomainDate);
            int day = Integer.valueOf(secondArrTime[2]) - 1;
            retDate = secondArrTime[0]+"-"+secondArrTime[1]+"-"+"0"+String.valueOf(day);
        }
        return retDate;
    }

}
